﻿using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.PICCNursingForm
{
    public partial class PICCNursingPrint : Form
    {
       #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        DateTime Start;
        DateTime End;
        #endregion

        #region 构造函数

        public PICCNursingPrint(PatientInfo patientInfo, DateTime StartDate, DateTime EndDate)
        {
            InitializeComponent();
            this.PatientInfo = patientInfo;
            Start = StartDate;
            End = EndDate;
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\PICC护理记录单.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }
        #endregion
        #region 获取链接报表事件
        public void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.PICCNursing, Start, End);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    PICCNursingRecord ps = XmlUtil.XMLToObject<PICCNursingRecord>(record1.NursingFormData, Encoding.UTF8);
                    dt.Rows.Add(
                        ps.RiQi,
                        ps.ShiJian,
                        ps.WaiLuChangDu,
                        ps.ShangBiWei,
                        ps.GuanDaoTongChang,
                        ps.GengHuanGanSuMao,
                        ps.GengHuanFuTie,
                        ps.ChongGuanYeTi,
                        ps.FengGuan,
                         ps.ChuanCiQingKuang.Split('#')[0].Equals("1") ? "√" : string.Empty,
                         ps.ChuanCiQingKuang.Split('#')[1].Equals("1") ? "√" : string.Empty,
                         ps.ChuanCiQingKuang.Split('#')[2].Equals("1") ? "√" : string.Empty,
                         ps.ChuanCiQingKuang.Split('#')[3].Equals("1") ? "√" : string.Empty,
                         ps.ChuanCiQingKuang.Split('#')[4].Equals("1") ? "√" : string.Empty,
                        ps.HuLiCuoShi,
                        ps.QianMing,
                        record1.PatientName,
                        PatientInfo.SEX,//SEX
                        PatientInfo.AGE,//AGE
                        PatientInfo.NURSE_CELL_NAME,//NURSE_CELL_NAME,
                        PatientInfo.DEPARTMENT_NAME,//DEPARTMENT_NAME
                        PatientInfo.BED_NO,//BED_NO,
                        PatientInfo.PATIENT_ID//PATIENT_ID
                        );
                  
                }
                dt.DefaultView.Sort = "RiQi ASC,ShiJian ASC";
                DataTable data = dt.DefaultView.ToTable();
                Utility.FillRecordToReport(Report, data);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion
        #region  创建列头
        private void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("RiQi", typeof(string));   //日期  
            dt.Columns.Add("ShiJian", typeof(string));   //时间  
            dt.Columns.Add("WaiLuChangDu", typeof(string)); //外露长度
            dt.Columns.Add("ShangBiWei", typeof(string));   //上臂围或腿围
            dt.Columns.Add("GuanDaoTongChang", typeof(string)); //管路是否通畅
            dt.Columns.Add("GengHuanGanSuMao", typeof(string)); //更换肝素帽
            dt.Columns.Add("GengHuanFuTie", typeof(string));  //更换敷贴
            dt.Columns.Add("ChongGuanYeTi", typeof(string));  //冲管液体
            dt.Columns.Add("FengGuan", typeof(string));  //封管
            dt.Columns.Add("ChuanCi1", typeof(string));  //穿刺部位情况--正常
            dt.Columns.Add("ChuanCi2", typeof(string));  //穿刺部位情况--渗血
            dt.Columns.Add("ChuanCi3", typeof(string));  //穿刺部位情况--渗液
            dt.Columns.Add("ChuanCi4", typeof(string));  //穿刺部位情况--红肿
            dt.Columns.Add("ChuanCi5", typeof(string));  //穿刺部位情况--其他
            dt.Columns.Add("HuLiCuoShi", typeof(string));  //护理措施
            dt.Columns.Add("QianMing", typeof(string));   //签名
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PatientId", typeof(string));
        }
        #endregion
        #region 窗体加载锐浪表单
        private void PICCNursingPrint_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
    }
}
